package sn.hn.demo;

import sn.hn.demo.highscore.HighScore;
import sn.hn.demo.image.ImageData;
import sn.hn.demo.sound.Music;
import sn.hn.demo.sound.Prefs;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnTouchListener {
	ImageView btnStart;
	ImageView btnSettings;
	ImageView btnHelp;
	ImageView btnHighScore;
	public final int DEFAULT_WIDTH = 800;
	public final int DEFAULT_HEIGHT = 480;
	public static float wp,hp;
	public static int SCREENWIDTH;
	public static int SCREENHEIGHT;
	public static DisplayMetrics METRICS;
	public static float X_UNIT;
	public static float Y_UNIT;
	public Point mSize;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        METRICS = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(METRICS);
        
        SCREENWIDTH = METRICS.widthPixels;
        SCREENHEIGHT = METRICS.heightPixels;
        wp = (float) SCREENWIDTH / DEFAULT_WIDTH;
		hp = (float) SCREENHEIGHT / DEFAULT_HEIGHT;
		Y_UNIT = (float)SCREENHEIGHT/48;
//		Log.d("ScreenSize","W: " + SCREENWIDTH + " H: " + SCREENHEIGHT +" hp: " + hp + " wp: " + wp);
		
        setContentView(R.layout.main);
        btnStart = (ImageView)findViewById(R.id.btnStart);
        btnStart.setLayoutParams(new LayoutParams(4*MainActivity.SCREENWIDTH/16,MainActivity.SCREENHEIGHT/16));
        setLayout(btnStart, (float)2.85*SCREENWIDTH/5, (float)1.2*SCREENHEIGHT/3, 0, 0);
        btnStart.setOnTouchListener(this);
        
        btnSettings = (ImageView)findViewById(R.id.btnSettings);
        btnSettings.setLayoutParams(new LayoutParams(4*MainActivity.SCREENWIDTH/16,MainActivity.SCREENHEIGHT/16));
        setLayout(btnSettings, (float)2.85*SCREENWIDTH/5, (float)6.7*SCREENHEIGHT/12, 0, 0);
        btnSettings.setOnTouchListener(this);
        
        btnHelp = (ImageView)findViewById(R.id.btnHelp);
        btnHelp.setLayoutParams(new LayoutParams(4*MainActivity.SCREENWIDTH/16,MainActivity.SCREENHEIGHT/16));
        setLayout(btnHelp, (float)2.85*SCREENWIDTH/5, (float)8.45*SCREENHEIGHT/12, 0, 0);
        btnHelp.setOnTouchListener(this);
        
        btnHighScore = (ImageView)findViewById(R.id.btnHighScore);
        btnHighScore.setLayoutParams(new LayoutParams(4*MainActivity.SCREENWIDTH/16,MainActivity.SCREENHEIGHT/16));
        setLayout(btnHighScore, (float)2.85*SCREENWIDTH/5, (float)10.4*SCREENHEIGHT/12, 0, 0);
        btnHighScore.setOnTouchListener(this);
        // Load data and config screen 
        ImageData.loadData(this);
        if(wp < 1 || hp < 1)
        	ImageData.resize();
        if(hp <=1)
        {
        	GameSurface.deltaPath = 1;
        }
		else
		{
			GameSurface.deltaPath = 2;
		}
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	Music.play(this, R.raw.u);
    	resetImage();
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	Music.stop(this);
    }
    
    public void resetImage()
    {
    	btnStart.setImageResource(R.drawable.batdau);
    	btnSettings.setImageResource(R.drawable.caidat);
    	btnHelp.setImageResource(R.drawable.huongdan);
    	btnHighScore.setImageResource(R.drawable.kyluc);
    }
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			switch (v.getId()) {
			case R.id.btnStart:
				btnStart.setImageResource(R.drawable.batdauclick);
				startActivity(new Intent(getBaseContext(), ShopActivity.class));
				break;
			case R.id.btnSettings:
				btnSettings.setImageResource(R.drawable.caidatclick);
				startActivity(new Intent(getBaseContext(), Prefs.class));
				break;
			case R.id.btnHelp:
				btnHelp.setImageResource(R.drawable.huongdanclick);
				showIntro();
				break;
			case R.id.btnHighScore:
				btnHighScore.setImageResource(R.drawable.kylucclick);
				startActivity(new Intent(getBaseContext(), HighScore.class));
				break;
			default:
				break;
			}
		}
		return false;
	}
	
	private void showIntro() {
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setTitle("Hướng dẫn");
		builder.setMessage("Chạm vào màn hình để di chuyển thuyền và thả câu!");
		builder.setPositiveButton("OK", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				resetImage();
			}
		});
		builder.show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Exit game");
			builder.setMessage("Do you want to exit?");
			builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			builder.setNegativeButton("Cancel", null);
			builder.show();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void showAboutDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("About");
		builder.setMessage("The game develop by CuongPX - Sunnet ITC");
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				resetImage();
			}
		});
		builder.show();
	}
	
	public static void setLayout(View v, float left, float top, float right,
			float bottom) {
		RelativeLayout.LayoutParams layoutparam = new RelativeLayout.LayoutParams(
				v.getLayoutParams().width, v.getLayoutParams().height);
		layoutparam.setMargins((int) left, (int) top, (int) right, (int) bottom);
		v.setLayoutParams(layoutparam);
	}
}